package com.enhinck.dbutilweb.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 传入jsonString串，生成对应JSON生成代码
 *
 * @author huenbin
 * @date 12/18/20 4:19 PM
 */
@Slf4j
public class JSONBeanUtil {


    public static void main(String[] args) {
        String jsonString = "{\"attachedList\":[],\"autoEnd\":0,\"deadline\":\"\",\"receiverRoles\":[1,2,3,4,5],\"taskAssignType\":1,\"authorityCode\":\"19902000000\",\"title\":\"55555\",\"templateId\":11384,\"chargePersons\":[{\"orgUserId\":74079196,\"orgUserName\":\"沙棘\",\"orgId\":10531}],\"chargerDeptList\":[],\"managers\":[],\"managerDeptList\":[],\"importId\":null,\"notifyWay\":1,\"tagIds\":[8900016,8600031],\"departIds\":[],\"sendObjectSource\":1,\"descriptionOssKey\":\"appoint/html/202012/f31d45730427996eccc1bca30c05a648yzgmho\"}";

        log.info("=======代码开始======\n\n{}\n====代码结束================", toJavaCode(jsonString));
    }


    public static String toJavaCode(String jsonString){
        Object object = JSONObject.parse(jsonString);
        final StringBuilder javaCodeBuilder = new StringBuilder();
        toJavaCode(javaCodeBuilder, object, "jsonObject");
        return javaCodeBuilder.toString();
    }

    public static void toJavaCode(final StringBuilder javaCodeBuilder, Object object, String objName) {
        if (object instanceof JSONObject) {
            javaCodeBuilder.append("JSONObject ").append(objName).append(" = new JSONObject();\n");
            JSONObject jsonObject = (JSONObject) object;
            Map<String, Object> map = jsonObject.getInnerMap();
            map.forEach((key, value) -> {
                if (value instanceof JSONObject||value instanceof JSONArray){
                    toJavaCode(javaCodeBuilder, value, key);
                    javaCodeBuilder.append(objName).append(".put(\"").append(key).append("\",").append(key).append(");\n");
                }else {
                    if (value instanceof String){
                        javaCodeBuilder.append(objName).append(".put(\"").append(key).append("\",\"").append(value).append("\");\n");
                    }else {
                        javaCodeBuilder.append(objName).append(".put(\"").append(key).append("\",").append(value).append(");\n");
                    }
                }
            });
        } else if (object instanceof JSONArray) {
            javaCodeBuilder.append("JSONArray ").append(objName).append(" = new JSONArray();\n");
            JSONArray jsonArray = (JSONArray) object;
            for (int i = 0; i < jsonArray.size(); i++) {
                Object childrenObj = jsonArray.get(i);
                String name = objName + (i+1);
                if (childrenObj instanceof JSONObject||childrenObj instanceof JSONArray){
                    toJavaCode(javaCodeBuilder, childrenObj, name);
                    javaCodeBuilder.append(objName).append(".add(").append(name).append(");\n");
                }else{
                    if (childrenObj instanceof String){
                        javaCodeBuilder.append(objName).append(".add(\"").append(childrenObj).append("\");\n");
                    }else {
                        javaCodeBuilder.append(objName).append(".add(").append(childrenObj).append(");\n");
                    }
                }
            }
        } else {
            if (object instanceof String){
                javaCodeBuilder.append("String ").append(objName).append(" = \"").append(object).append("\";\n");
            }else {
                javaCodeBuilder.append("Object ").append(objName).append(" = ").append(object).append(";\n");
            }
        }
    }


}
